package cn.bqjr.eily.samples.common.shiro;

import cn.bqjr.eily.samples.entity.po.User;
import cn.bqjr.eily.samples.facade.IUserFacade;
import cn.bqjr.eily.samples.service.base.IUserService;
import cn.bqjr.eily.shiro.spring.boot.IShiroUserService;
import cn.bqjr.eily.shiro.spring.boot.entity.ShiroUser;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class ShiroUserServiceImpl implements IShiroUserService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserFacade userFacade;

    @Override
    public ShiroUser selectByLoginId(Serializable loginId) {
        User user = userService.selectOne(new EntityWrapper<User>().eq(User.LOGINID, loginId));
        if (null == user) {
            return null;
        }
        return new ShiroUser().setLocked(user.isLocked())
                .setValid(user.isValid())
                .setPassword(user.getPassword())
                .setUserId(user.getUserId());
    }

    @Override
    public Set<String> selectUserPerms(Serializable userId) {
        return userFacade.selectUserPerms(Long.valueOf(userId.toString()));
    }

    @Override
    public List<String> selectUserRoleIdList(Serializable userId) {
        List<String> roleIdList = new ArrayList<>();
        userFacade.selectUserRoleIdList(Long.valueOf(userId.toString()))
                .forEach(id -> roleIdList.add(String.valueOf(id)));
        return roleIdList;
    }
}
